package pers.qianyu.diytomcat.catalina;

import pers.qianyu.diytomcat.util.*;

import java.util.*;

/**
 * @author mizzle rain
 * @date 2021-01-07 16:35
 */
public class Engine {
    private String defaultHost;
    private List<Host> hosts;
    private Service service;

    public Engine(Service service) {
        this.defaultHost = ServerXmlUtil.getEngineDefaultHost();
        this.hosts = ServerXmlUtil.getHosts(this);
        this.service = service;
        checkDefault();
    }

    private void checkDefault() {
        if (defaultHost == null) {
            throw new RuntimeException(String.format("the defaultHost %s does not exist!", defaultHost));
        }
    }

    public Host getDefaultHost() {
        for (Host host : hosts) {
            if (host.getName().equals(defaultHost)) {
                return host;
            }
        }
        return null;
    }

    /************** automatic generated *****************/
    public List<Host> getHosts() {
        return hosts;
    }

    public void setHosts(List<Host> hosts) {
        this.hosts = hosts;
    }

    public Service getService() {
        return service;
    }

    public void setService(Service service) {
        this.service = service;
    }
}
